<template>
  <a-modal
    title="配置网关所属站点与名称"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['id']"/>
        </a-form-item>

        <a-form-item
          label="所属站点"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select style="width: 100%" placeholder="请选择所属站点" v-decorator="['stationId', {rules: [{ required: true, message: '请选择所属站点！' }]}]" >
            <a-select-option v-for="(item,index) in stationTree" :key="index" :value="item.id" >{{ item.stationName }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="网关名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入网关名称" v-decorator="['gatewayName', {rules: [{required: true, message: '请输入网关名称！'}]}]" />
        </a-form-item>

        <a-form-item
          label="网关编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入网关编号" :disabled="true" v-decorator="['gatewayCode', {rules: [{required: true, message: '请输入网关编号！'}]}]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import { list } from '@/api/modular/main/bizstation/bizStationManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        stationTree: [],
        treeLoading: true
      }
    },
    methods: {
      edit (record) {
        this.visible = true
        this.getStationTree()
        setTimeout(() => {
          this.form.setFieldsValue({
            id: record.id,
            gatewayCode: record.gatewayCode
          })
        }, 100)
      },
      getStationTree () {
        list().then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.stationTree = res.data
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$router.push({ path: '/gateway' })
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
